package org.springblade.modules.admin.service.approve.strategy;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.redis.cache.BladeRedis;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.modules.admin.entity.BusinessApprove;
import org.springblade.modules.admin.enums.ApproveStatusEnum;
import org.springblade.modules.admin.service.IBusinessApproveService;
import org.springblade.modules.admin.service.IMaintainPlanContentService;
import org.springblade.modules.admin.service.WeChatDialogueService;
import org.springblade.modules.admin.vo.BusinessApproveVO;
import org.springblade.modules.system.service.IUserService;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;

import java.util.Map;

/**
 * @Description 审批业务流程
 * @Author ysq
 * @Date 2021/12/7
 **/
public interface ApproveBusinessFlow {

	IBusinessApproveService businessApproveService = SpringUtil.getBean(IBusinessApproveService.class);
	IMaintainPlanContentService maintainPlanContentService = SpringUtil.getBean(IMaintainPlanContentService.class);
	PlatformTransactionManager platformTransactionManager = SpringUtil.getBean(PlatformTransactionManager.class);
	TransactionDefinition transactionDefinition = SpringUtil.getBean(TransactionDefinition.class);
	WeChatDialogueService weChatDialogueService = SpringUtil.getBean(WeChatDialogueService.class);
	BladeRedis bladeRedis = SpringUtil.getBean(BladeRedis.class);
	IUserService userService = SpringUtil.getBean(IUserService.class);

	/**
	 * 分页
	 * @param param
	 * @param query
	 * @return
	 */
	default IPage<Map<String, Object>> page(Map<String, Object> param, Query query) {
		QueryWrapper<BusinessApprove> queryWrapper = Condition.getQueryWrapper(param, BusinessApprove.class);
		return businessApproveService.pageMaps(Condition.getPage(query), queryWrapper);
	}

	/**
	 * 详情
	 * @param businessApproveId
	 * @return
	 */
	default Object detail(Long businessApproveId) {
		return businessApproveService.getById(businessApproveId);
	}

	/**
	 * 添加审批流程
	 * @param businessApprove
	 * @return
	 */
	default boolean addApprove(BusinessApproveVO businessApprove) {
		return businessApproveService.save(businessApprove);
	}

	/**
	 * 执行审批
	 * @param businessApproveVO
	 * @return
	 */
	default boolean approve(BusinessApproveVO businessApproveVO) {
		BusinessApprove businessApprove = businessApproveService.getById(businessApproveVO.getId());
		if (Func.isEmpty(businessApprove)) {
			throw new ServiceException("未找到相关流程!");
		}
		if (businessApprove.getApproveStatus() != ApproveStatusEnum.PENDING.getId() && businessApprove.getApproveStatus() != ApproveStatusEnum.RETURN.getId()) {
			throw new ServiceException("流程已审批，不能重复审批!");
		}

		businessApprove.setApproveStatus(businessApproveVO.getApproveStatus());
		businessApprove.setRemark(businessApproveVO.getRemark());
		return businessApproveService.updateById(businessApprove);
	}

	/**
	 * 催办
	 * @param businessApproveId
	 * @return
	 */
	default boolean reminder(Long businessApproveId) {
		return false;
	}

	/**
	 * 撤回
	 * @param businessApproveId
	 * @return
	 */
	default boolean cancel(Long businessApproveId) {
		BusinessApprove businessApprove = businessApproveService.getById(businessApproveId);
		if (Func.isEmpty(businessApprove)) {
			throw new ServiceException("未找到相关流程!");
		}
		businessApprove.setApproveStatus(ApproveStatusEnum.CANCEL.getId());
		return businessApproveService.updateById(businessApprove);
	}

}
